package com.yunao.web.controller;

import com.yunao.commons.Exception.YunaoException;
import com.yunao.commons.enums.ResultEnum;
import com.yunao.commons.utils.ResultVOUtil;
import com.yunao.commons.vo.ResultVO;
import com.yunao.entity.Organization;
import com.yunao.service.OrganizationService;
import com.yunao.web.annotation.UserLoginToken;
import com.yunao.web.convert.OrganizationForm2OrganizationConverter;
import com.yunao.web.form.OrganizationForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/user/organ/")
@Slf4j
public class OrganController {
    @Autowired
    private OrganizationService organService;

    /**
     * showdoc
     * @catalog API文档/组织模块
     * @title 组织列表
     * @description 获取所有组织列表的接口
     * @method get
     * @url /user/organ/list
     * @return {code: 0,msg: "成功",data: [{organId: 1,organName: "北京迅奥",organShortName: "cnxunao",organPerson: "dada",organEmail: "131879@qq.com",organMobile: "18234112231",organTelephone: "885214610",organUserNum: 0,organUserCeiling: 10,organSubjectNum: 0,organSubjectCeiling: 10,organCreateDate: "2019-07-10 18:35:24",organUpdateDate: "2019-07-10 18:35:24",organExpireDate: "2019-12-10 18:35:24",organAccountType: 0,organLocaltion: "北京",organTrade: "银行"}]}
     * @remark data中是一个组织数组
     * @number 01
     */
    @UserLoginToken
    @GetMapping("/list")
    public ResultVO list(){
        List<Organization> organizationList = organService.findAll();
        return ResultVOUtil.success(organizationList);
    }

    /**
     * showdoc
     * @catalog API文档/组织模块
     * @title 组织查询
     * @description 通过id查询单个组织
     * @method get
     * @url /user/organ/findone
     * @param organId 必选 int 组织id
     * @return {code: 0,msg: "成功",data: {organId: 1,organName: "北京迅奥",organShortName: "cnxunao",organPerson: "dada",organEmail: "131879@qq.com",organMobile: "18234112231",organTelephone: "885214610",organUserNum: 0,organUserCeiling: 10,organSubjectNum: 0,organSubjectCeiling: 10,organCreateDate: "2019-07-10 18:35:24",organUpdateDate: "2019-07-10 18:35:24",organExpireDate: "2019-12-10 18:35:24",organAccountType: 0,organLocaltion: "北京",organTrade: "银行"}}
     * @remark
     * @number 02
     */
    @UserLoginToken
    @GetMapping("/findone")
    public ResultVO findone(@RequestParam Integer organId){
        Organization organ = organService.findOne(organId);
        if (organ==null)
            throw new YunaoException(ResultEnum.SUBJECT_NOT_EXIST);
        return ResultVOUtil.success(organ);
    }

    @UserLoginToken
    @PostMapping("/save")
    public ResultVO save(@Valid OrganizationForm organForm,
                         BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("【创建组织】参数不正确");
            throw new YunaoException(ResultEnum.PARAM_ERROR.getCode(),
                    bindingResult.getFieldError().getDefaultMessage());
        }
        Organization organization = OrganizationForm2OrganizationConverter.convert(organForm);
        organService.save(organization);
        return ResultVOUtil.success();
    }

    /**
     * showdoc
     * @catalog API文档/组织模块
     * @title 删除组织
     * @description 通过id删除组织
     * @method get
     * @url /user/organ/delete
     * @param organId 必选 int 组织id
     * @return {code: 0,msg: "成功",data: [{organId: 1,organName: "北京迅奥",organShortName: "cnxunao",organPerson: "dada",organEmail: "131879@qq.com",organMobile: "18234112231",organTelephone: "885214610",organUserNum: 0,organUserCeiling: 10,organSubjectNum: 0,organSubjectCeiling: 10,organCreateDate: "2019-07-10 18:35:24",organUpdateDate: "2019-07-10 18:35:24",organExpireDate: "2019-12-10 18:35:24",organAccountType: 0,organLocaltion: "北京",organTrade: "银行"}]}
     * @remark
     * @number 04
     */
    @UserLoginToken
    @GetMapping("/delete")
    public ResultVO delete(Integer organId){
        organService.delete(organId);
        return ResultVOUtil.success();
    }
}
